import {
  AUTH_TREE,
  DELETE_SYS_USER,
  LOGIN,
  USER_INFO,
  Q_SYS_OPLOG_LIST,
  Q_SYS_ROLE_LIST,
  Q_SYS_USER_LIST,
  ROUTES,
  SAVE_SYS_ROLE,
  SAVE_SYS_USER
} from '@/services/api'
import {METHOD, removeAuthorization, request} from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(name, password) {
  return request(LOGIN, METHOD.POST, {
    username: name,
    password: password
  })
}


export const queryUserList = (params) => {
  return request(Q_SYS_USER_LIST, METHOD.GET, params)
}
export const queryRoleList = (params) => {
  return request(Q_SYS_ROLE_LIST, METHOD.GET, params)
}


export const saveUser = (params) => {
  return request(SAVE_SYS_USER, METHOD.POST, params)
}

export const deleteUserById = (params) => {
  return request(DELETE_SYS_USER, METHOD.DELETE, params)
}


export const saveRole = (params) => {
  return request(SAVE_SYS_ROLE, METHOD.POST, params)
}

export async function queryUserInfo() {
  return request(USER_INFO, METHOD.POST, {})
}

export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}

export async function getAuths(param) {
  return request(AUTH_TREE, METHOD.GET, param)
}

export const queryOpLogList = (params) => {
  return request(Q_SYS_OPLOG_LIST, METHOD.GET, params)
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}
export default {
  login,
  logout,
  getRoutesConfig
}
